package main

import (
	"fmt"
	"os"
)

/*
@author: liujun
@file: atm.go
@time: 2022/12/2 14:43
*/
type User struct {
	name string
	passwd string
	balance int
}
type ATM struct {
	bank string
	balance int
}
var users = map[string]User{}
func main() {
	atm := ATM{
		bank:    "农业银行",
		balance: 100000,
	}
	for {
		fmt.Println("1.登录")
		fmt.Println("2.注册")
		var option string
		fmt.Print("请输入你的选择(按q退出)：")
		fmt.Scan(&option)
		switch option {
		case "q":
			os.Exit(3)
		case "1":
			for {
				var name string
				var password string
				fmt.Print("请输入用户名：")
				fmt.Scan(&name)
				fmt.Print("请输入密码：")
				fmt.Scan(&password)
				_, ok := users[name]
				if ok && users[name].passwd == password {
					fmt.Println("登陆成功！")
					fmt.Println("欢迎来到", atm.bank)
					for {
						fmt.Print("1.存钱\n2.取钱\n3.查看余额")
						var op string
						fmt.Print("请输入你的选择(按q退出)：")
						fmt.Scan(&op)
						switch op {
						case "q":
							os.Exit(3)
						case "1":
							var money int
							fmt.Print("请输入存款金额：")
							fmt.Scan(&money)
							atm.save_money(users[name], money)
						case "2":
							var money int
							fmt.Print("请输入取款金额：")
							fmt.Scan(&money)
							atm.draw_money(users[name], money)
						case "3":
							fmt.Printf("您的余额为%d元\n", users[name].balance)
						default:
							fmt.Println("输入不合法")
						}
					}
				} else {
					fmt.Println("登陆失败，用户名或密码错误")
				}
			}
		case "2":
			var name string
			var passwd string
			var money int
			for {
				fmt.Print("请输入用户名：")
				fmt.Scan(&name)
				fmt.Print("请输入密码：")
				fmt.Scan(&passwd)
				fmt.Print("充值初始金额：")
				fmt.Scan(&money)
				_, ok := users[name]
				if ok {
					fmt.Println("用户名已存在")
				} else {
					newuser := User{
						name:    name,
						passwd:  passwd,
						balance: money,
					}
					users[name] = newuser
					break
				}
			}
		default:
			fmt.Println("输入不合法")
		}
	}
}
func (atm *ATM) save_money(user User, money int) int {
	user.balance += money
	atm.balance += money
	fmt.Println("存入成功！余额为：",user.balance)
	users[user.name]=user
	return atm.balance
}
func (atm *ATM) draw_money(user User, money int)  {
	if money > user.balance {
		fmt.Println("用户余额不足")
	}else if money > atm.balance {
		fmt.Println("ATM余额不足")
	}else {
		user.balance -= money
		atm.balance -= money
		fmt.Println("余额为：", user.balance, "ATM")
		users[user.name] = user
	}
}
